package com.lethe.medical.base.filter;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.annotation.WebInitParam;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

//@WebFilter(filterName = "BResourcesFilter",urlPatterns={"/*"})
public class BResourcesFilter implements Filter {
    //获取上下文
    private String ctxPath;

    public void destroy() {
    }

    public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException {
        HttpServletRequest request = (HttpServletRequest) req;
        HttpServletResponse response = (HttpServletResponse) resp;

        String uri = request.getRequestURI();
        System.out.println("ResourcesFilter里面的uri = "+uri);
         String statics = "statics";
        //登录页
         String login = "login";
        //默认首页
         String medical = "medical";

        //文件上传
         String file = "file";

        if(uri.indexOf(statics)>-1){
            System.out.println("ResourcesFilter的if方法执行了");
            chain.doFilter(req, resp);

            return;
        }
        if(uri.indexOf(login)>-1){
            System.out.println("ResourcesFilter的if方法执行了");
            chain.doFilter(req, resp);

            return;
        }

        if(uri.indexOf(file)>-1){
            System.out.println("ResourcesFilter的if方法执行了");
            chain.doFilter(req, resp);

           return;
        }


        if(medical.equals(uri) || (medical+"/").equals(uri)||("/"+medical+"/").equals(uri)){
            response.sendRedirect(request.getContextPath()+"/login.jsp");
            return;
        }
        //chain.doFilter(req,resp);//上面要return，这里要chain.doFIlter放过才行
    }

    public void init(FilterConfig config) throws ServletException {
        ctxPath = config.getServletContext().getContextPath();
    }

}
